package com.kczd.jinlan.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.kczd.jinlan.R;
import com.kczd.jinlan.activity.ClassifydetilActivity;
import com.kczd.jinlan.activity.DetailsFoodActivity;
import com.kczd.jinlan.activity.SearchAreaActivity;
import com.kczd.jinlan.activity.SearchClassifyActivity;
import com.kczd.jinlan.adapter.normaladapter.ClassifyAdapter;
import com.kczd.jinlan.adapter.TejiaAdapter;
import com.kczd.jinlan.model.Classify;
import com.kczd.jinlan.model.Tejia;
import com.kczd.jinlan.myview.FullyGridLayoutManager;
import com.kczd.jinlan.myview.MenuPopwindow;
import com.kczd.jinlan.myview.NoScrollGridView;
import com.kczd.jinlan.myview.PicsView;
import com.kczd.jinlan.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

import static android.app.Activity.RESULT_OK;
import static com.kczd.jinlan.R.id.picsviewpager;

/**
 * yang_g
 * 商城模块
 * Created by Administrator on 2017/5/15.
 */

public class ShopPingFragment extends Fragment implements View.OnClickListener {
    private View view;
    int[] imgs = {R.mipmap.shouye_lunbo, R.mipmap.shouye_lunbo, R.mipmap.shouye_lunbo, R.mipmap.shouye_lunbo, R.mipmap.shouye_lunbo,
            R.mipmap.shouye_lunbo};
    String[] titles = {"我", "爱", "金", "兰", "蔬", "菜"};
    private PicsView mPicsviewpager;
    private NoScrollGridView mGridView;
    public static List<Classify> minelist;
    private List<Tejia> telist;

    private RecyclerView recyclerView;
    private EditText mEdit;
    private TextView mArea;

    private AMapLocationClient mLocationClient;
    private TextView mTejiatuijian;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.shopping_fragment, null);
        initViewPager();
        initGradView();
        initReView();

        initView();
        initLocation();
        return view;
    }

    private void initLocation() {
        mLocationClient = new AMapLocationClient(getActivity());
        AMapLocationClientOption option = new AMapLocationClientOption();
        option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        option.setOnceLocation(true);
        mLocationClient.setLocationOption(option);
        mLocationClient.setLocationListener(new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {
                if (aMapLocation != null) {
                    if (aMapLocation.getErrorCode() == 0) {
                        String city = aMapLocation.getCity();
                        String district = aMapLocation.getDistrict();
                        Log.e("onLocationChanged", "city: " + city);
                        Log.e("onLocationChanged", "district: " + district);
                        String location = StringUtils.extractLocation(city, district);
                        mArea.setText(location);
                        mArea.setTextColor(Color.WHITE);
                    } else {
                        //定位失败
                        mArea.setText("乌鲁木齐");
                        mArea.setTextColor(Color.WHITE);
                    }
                }
            }
        });
        mLocationClient.startLocation();
    }

    private void initView() {
        mEdit = (EditText) view.findViewById(R.id.et_search);//搜索
        mEdit.setOnClickListener(this);
        mArea = (TextView) view.findViewById(R.id.id_area);//选择区域
        mArea.setOnClickListener(this);
        view.findViewById(R.id.id_xiaoxi).setOnClickListener(this);
        mTejiatuijian = (TextView) view.findViewById(R.id.id_tejiatuijian);
        mTejiatuijian.setCompoundDrawablePadding(6);//修改drawable与文字的间距是6
        view.findViewById(R.id.id_more).setOnClickListener(this);
    }

    private void initReView() {
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
//        //RecycleView 增加边距
//        int spacingInPixels = 10;
//        recyclerView.addItemDecoration(new SpacesItemDecoration(spacingInPixels));

        FullyGridLayoutManager linearLayoutManager = new FullyGridLayoutManager(getActivity(), 2);
        recyclerView.setNestedScrollingEnabled(false);
        //设置布局管理器
        recyclerView.setLayoutManager(linearLayoutManager);

        TejiaAdapter tejia = new TejiaAdapter(getActivity(), telist);

        /**
         * 特价推荐跳转页面
         */
        tejia.setOnItemClickLitener(new TejiaAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(getActivity(), position + "", Toast.LENGTH_SHORT)
                        .show();
                startActivity(new Intent(getActivity(), DetailsFoodActivity.class));
            }
        });
        recyclerView.setAdapter(tejia);
    }

    private void initData() {
        minelist = new ArrayList<Classify>();
        minelist.add(new Classify("蔬菜", R.mipmap.sucai));
        minelist.add(new Classify("水果", R.mipmap.shuiguo));
        minelist.add(new Classify("海鲜", R.mipmap.haixian));
        minelist.add(new Classify("特价区", R.mipmap.tejiaqu));
        minelist.add(new Classify("蛋类", R.mipmap.danlei));
        minelist.add(new Classify("肉禽", R.mipmap.rouqing));
        minelist.add(new Classify("积分商城", R.mipmap.jifengshangcheng));
        minelist.add(new Classify("畅销区", R.mipmap.changxiaoqu));
    }

    private void initGradView() {
        initData();
        initReData();
        mGridView = (NoScrollGridView) view.findViewById(R.id.me_item_gridview);
        mGridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
        ClassifyAdapter mineActivity = new ClassifyAdapter(minelist);
        mGridView.setAdapter(mineActivity);
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), ClassifydetilActivity.class);
                switch (position) {
                    case 0://蔬菜
                        intent.putExtra("tobar", "蔬菜");
                        startActivity(intent);
                        break;
                    case 1://水果
                        intent.putExtra("tobar", "水果");
                        startActivity(intent);
                        break;
                    case 2://海鲜
                        intent.putExtra("tobar", "海鲜");
                        startActivity(intent);
                        break;
                    case 3://特价区
                        intent.putExtra("tobar", "特价区");
                        startActivity(intent);
                        break;
                    case 4://蛋类
                        intent.putExtra("tobar", "蛋类");
                        startActivity(intent);
                        break;
                    case 5://肉禽
                        intent.putExtra("tobar", "肉禽");
                        startActivity(intent);
                        break;
                    case 6://积分商城
                        intent.putExtra("tobar", "积分商城");
                        startActivity(intent);
                        break;
                    case 7://畅销区
                        intent.putExtra("tobar", "畅销区");
                        startActivity(intent);
                        break;
                }
            }
        });
    }

    private void initReData() {
        telist = new ArrayList<Tejia>();
        telist.add(new Tejia("西兰花", R.mipmap.xilanhua, "¥ 12", "¥ 26"));
        telist.add(new Tejia("西兰花", R.mipmap.tuceng1, "¥ 12", "¥ 26"));
        telist.add(new Tejia("西兰花", R.mipmap.xilanhua, "¥ 12", "¥ 26"));
        telist.add(new Tejia("西兰花", R.mipmap.tuceng1, "¥ 12", "¥ 26"));
        telist.add(new Tejia("西兰花", R.mipmap.xilanhua, "¥ 12", "¥ 26"));
        telist.add(new Tejia("西兰花", R.mipmap.tuceng1, "¥ 12", "¥ 26"));
        telist.add(new Tejia("西兰花", R.mipmap.xilanhua, "¥ 12", "¥ 26"));
        telist.add(new Tejia("西兰花", R.mipmap.tuceng1, "¥ 12", "¥ 26"));
    }

    private void initViewPager() {
        mPicsviewpager = (PicsView) view.findViewById(picsviewpager);

        List<ImageView> imgList = new ArrayList<ImageView>();
        for (int i = 0; i < imgs.length; i++) {
            ImageView iv = new ImageView(getActivity());
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            iv.setImageResource(imgs[i]);
            imgList.add(iv);
        }
        // 初始化数据
        mPicsviewpager.setTitlesAndImages(titles, imgList);

        // 设置点击事件
        mPicsviewpager.setOnLunBoClickListener(new PicsView.OnLunBoClickListener() {

            @Override
            public void clickLunbo(int position) {
                System.out.println("点击有效");
                Toast.makeText(getActivity(), "点击有效，位置为：" + position, Toast.LENGTH_SHORT).show();
            }
        });
        // 设置文字的颜色，透明即不可见
        // picsviewpager.setLlBackgroundAlph(color.transparent);
        // 设置文字的背景，默认半透明，可以设置不可见
        //picsviewpager.setTvTitleVisibility(View.GONE);
    }


    @Override
    public void onPause() {
        // 停止图片轮播
        mPicsviewpager.stopAutoScroll();
        super.onPause();
    }

    @Override
    public void onResume() {
        // 开启图片轮播
        mPicsviewpager.startAutoScroll();
        super.onResume();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.et_search://跳转到搜寻页面
                startActivity(new Intent(getActivity(), SearchClassifyActivity.class));
                break;
            case R.id.id_area://跳转到搜寻地址页面
                Intent intent = new Intent(getActivity(), SearchAreaActivity.class);
                startActivityForResult(intent, 1);
                break;
            case R.id.id_xiaoxi://右上角的actionbar
                MenuPopwindow popWindow = new MenuPopwindow(getActivity());
                popWindow.showPopupWindow(view.findViewById(R.id.id_xiaoxi));
                break;
            case R.id.id_more://更多按钮
                Intent intents = new Intent(getActivity(), ClassifydetilActivity.class);
                intents.putExtra("tobar", "特价推荐");
                startActivity(intents);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    String returnedData = data.getStringExtra(SearchAreaActivity.KEY_PICKED_CITY);
                    mArea.setText(returnedData);
                }
                break;
        }
    }

}
